package com.yiren.dbaa.module.device.presenter;

import android.app.Activity;
import android.os.Build;

import com.hjq.permissions.Permission;
import com.yiren.backstage.currency.constant.SignalGConstant;
import com.yiren.backstage.currency.utils.StringUtil;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.common.contract.PermissionContract;
import com.yiren.dbaa.module.common.model.PermissionModel;
import com.yiren.dbaa.module.device.contract.BlueToothContract;
import com.yiren.dbaa.module.device.model.BlueToothModel;
import com.yiren.dbaa.view.toast.MessageBox;

import org.apache.commons.lang.StringUtils;

import java.util.List;

/**
 * Created by WANGRONGXIANG
 * on 2022/7/1
 * Describe
 */
public class BlueToothPresenter implements BlueToothContract.Presenter {

    private Activity activity;
    private PermissionContract.Model permissionModel;
    private BlueToothContract.Model bluetoothModel;
    private String[] permissions = {
            Permission.BLUETOOTH_SCAN,
            Permission.BLUETOOTH_ADVERTISE,
            Permission.BLUETOOTH_CONNECT
    };

    public BlueToothPresenter(Activity activity) {
        this.activity = activity;
        permissionModel = new PermissionModel(activity);
        bluetoothModel = new BlueToothModel(activity);
    }

    @Override
    public void getBluetoothSpinner(BlueToothContract.View view) {
        permissionModel.requestPermissions(permissions, Build.VERSION_CODES.S, new CommonCallBack() {
            @Override
            public void success(Object result) {
                bluetoothModel.getDeviceList(new CommonCallBack() {
                    @Override
                    public void success(Object result) {
                        List<String> deviceList = (List<String>) result;
                        view.setBluetoothSpinner(deviceList);
                    }
                });
            }

            @Override
            public void fail(String error) {
                MessageBox.show(activity, error);
            }
        });
    }

    @Override
    public void openBluetooth(String btDeviceName) {
        if (SignalGConstant.isOpen) return;
        new Thread(() -> {
            bluetoothModel.connectBluetooth(btDeviceName, new CommonCallBack() {
                @Override
                public void success(Object result) {
                    MessageBox.show(activity, (String) result);
                }

                @Override
                public void fail(String error) {
                    MessageBox.show(activity, error);
                }

                @Override
                public void complete() {
                    //发送使用蓝牙路径的指令
                    bluetoothModel.sendData("AT+PATH=BT\r\n".getBytes());
                }
            });
        }).start();
    }

    @Override
    public void closeBluetooth() {
        bluetoothModel.disconnectBluetooth(new CommonCallBack() {
            @Override
            public void success(Object result) {
                MessageBox.show(activity, (String) result);
            }
        });
    }

    @Override
    public void sendOrder(String order, boolean isHex, boolean isRet) {
        if (!SignalGConstant.isOpen) {
            MessageBox.show(activity, "蓝牙未打开");
            return;
        }
        byte[] data = null;
        if (StringUtils.isEmpty(order)) {
            MessageBox.show(activity, "发送数据不能为空");
            return;
        }
        if (isHex) {
            data = StringUtil.hexStringToByte(order);
            if (data == null) {
                MessageBox.show(activity, "发送数据格式不对");
                return;
            }
        } else {
            if (isRet)
                data = (order + "\r\n").getBytes();
        }
        //发送数据
        bluetoothModel.sendData(data);
    }
}
